<script type="text/javascript"><!--//--><![CDATA[//><!--
var LOVJenisProduk_xx = function(ao_option){
	try{
		if(ao_option==undefined){ ao_option={}; }
		var lf_onSelect	= (ao_option.onSelect==undefined || typeof(ao_option.onSelect)!='function') ? function(){} : ao_option.onSelect;
		
		// Create Popup
		var $popup	= openPopup({ title:'Daftar Produk' , width:700, height:370 });
		var ls_id	= $popup.attr('id');
		
		// Append Datagrid Container
		var ls_gridName	= 'grid_'+ls_id;
		$popup	.html(	$('<div>')	.attr('id', 'tb'+ls_id).attr('style','padding:5px;height:auto;')
									.html(	$('<div>').html($('<form>')	.attr('id','fm_search_'+ls_id).data('rel',ls_id).submit(function(e){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).data('rel')).serializeObject()); e.preventDefault(); })
																		.html('<table class="tb_form" style="margin-bottom:0px"><tr><td width="100">Keyword</td><td><input type="text" name="keyword" class="textfield medium" /><a href="#" class="easyui-linkbutton" iconCls="icon-search" id="btn_search'+ls_id+'" rel="'+ls_id+'">Search</a></td></tr></table>')
															)
											)
						)
				.append($('<div>').attr('id', ls_gridName).attr('toolbar', '#tb'+$popup.attr('id')));
		
		$('#btn_search'+ls_id).click(function(){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).attr('rel')).serializeObject()); return false; });
		$('#btn_search'+ls_id).linkbutton({  
			plain:true  
		}); 
		
		// Build Datagrid
		var ls_param = (ao_option.param==undefined ? '' : ao_option.param);
		$('#'+ls_gridName).datagrid({  
			url:gs_path+'/isp_customer_kontrak/getdata4/'+ls_param,
			queryParams: $('#fm_search_'+ls_id).serializeObject(),
			height:337,
			pagination:true,
			singleSelect:true,
			columns:[[
				{ field : 'id_produk', title : 'Kode ID Produk', width : 100, sortable:true },
				{ field : 'nama_jenis_barang', title : 'Nama Produk', width : 100, sortable:true },
				{ field : 'bandwidth', title : 'Bandwidth', width : 300, sortable:true },
				{ field : 'harga', title : 'Harga Produk', width : 300, sortable:true },
			]],
			onSelect:function(rowIndex, rowData){ lf_onSelect(rowData); closePopup(); }
		});
		
	} catch (err) { alert('Func. LOVSampleReference :\n' + err); }
};

	var grid_reload = function(){
		$('#datagrid').datagrid('reload', $('#fm_search').serializeObject());
	};

	$(document).ready(function() {

		// Setup Datagrid
		$('#datagrid').datagrid({  
			url:gs_path+'/'+controller+'/getdata', 
			queryParams: $('#fm_search').serializeObject(),
			height:340,
			pagination:true,
			columns:[[
				{ field : 'ck', checkbox:true, width: 40, align: 'center' },
				{ field : 'kode_kontrak', title : 'Kode Kontrak', width : 170, sortable:true },
				{ field : 'kode_customer', title : 'Kode Client', width : 120, sortable:true },
				{ field : 'nama_customer', title : 'Nama Client', width : 120, sortable:true },
				{ field : 'kode_jenis_barang', title : 'Kode Produk', width : 100, sortable:true },
				{ field : 'status', title : 'Status Berlangganan', width : 100, sortable:true },
				{ field : 'bandwidth', title : 'Bandwidth', width : 200, sortable:true },
				{ field : 'tgl_awal_kontrak', title : 'Tgl Awal Kontrak', width : 100, sortable:true },
				{ field : 'tgl_akhir_kontrak', title : 'Tgl Akhir Kontrak', width : 150, sortable:true },
				{ field : 'tgl_aktifasi', title : 'Tgl Aktifasi', width : 100, sortable:true },
				{ field : 'marketing_fee', title : 'Marketing Fee', width : 100, sortable:true },
				{ field : 'keterangan', title : 'Keterangan', width : 150, sortable:true }
			]]
		});		


		
		// Setup Form
		$('#form_master').setupForm([			
			{ name: 'kode_kontrak', type: 'text', size: 'long', maxlength: 50, noempty:true },
			{ name: 'kode_customer', type: 'text', size: 'short' },
			{ name: 'nama_customer', type: 'text', size:'medium', maxlength: 255 },
			{ name: 'tgl_awal_kontrak', type: 'date', noempty:true ,value:new Date() },
			{ name: 'tgl_akhir_kontrak', type: 'date', noempty:true ,value:new Date() },
			{ name: 'tgl_aktifasi', type: 'date', noempty:true  ,value:new Date()},
			{ name: 'marketing_fee', type: 'text', size:'short'},
			{ name: 'harga', type: 'text', size: 'medium', maxlength: 40},			
			{ name: 'keterangan', type: 'textarea', size:'long', rows:3 },
			{ name: 'urutan', value:'-', type: 'text', size: 'long', maxlength: 255 },
			{ name: 'nama_jenis_barang', value:'-', type: 'text', size: 'long', maxlength: 255, noempty:true},
			{ name: 'kode_jenis_barang', type: 'lov', size: 'short', noempty:true ,
				onClearButton:function(){
					$(this).setval(''); 
					$('#bandwidth').setval('');
					$('#nama_jenis_barang').setval(''); 
					$('#bandwidth').setval('');
					$('#harga').setval('');
				},
				onActionButton:function(){ 
					LOVJenisProduk_xx({ onSelect:function(row){ 
						$('#kode_jenis_barang').setval(row.id_produk); 
						$('#nama_jenis_barang').setval(row.nama_jenis_barang); 
						$('#bandwidth').setval(row.bandwidth);
						$('#harga').setval(row.harga);
					} }); 
				}
			},
			{ name: 'bandwidth', type: 'text', size: 'medium', maxlength: 40, readonly:true },
			{ name: 'harga', type: 'text', size: 'medium', maxlength: 30, noempty:true },
			/*{ name: 'nama_inventori', type: 'lov', size: 'short',
				onClearButton:function(){
					$(this).setval(''); 
					$('#banyak').setval(''); 
				},
				onActionButton:function(){ 
					LOVJenisProduk2_xx({ onSelect:function(row){ 
						$('#nama_inventori').setval(row.nama_inventori); 
						$('#stock').setval(row.banyak);
						$('#banyak').setval(1); //row.banyak 
						$('#produk_harga').setval(row.harga); 
					} }); 
				}
			},
			{ name: 'banyak', type: 'text', size: 'medium', maxlength: 40 },
			{ name: 'produk_harga', type: 'text', size: 'medium', maxlength: 30, noempty:true, readonly:true },
			{ name: 'stock', type: 'text', size: 'medium', maxlength: 30, noempty:true, readonly:true },
			{ name: 'total_biaya', type: 'text', size: 'medium', maxlength: 30, noempty:true, readonly:true },
			{ name: 'biaya_pendaftaran', type: 'text', size: 'medium', maxlength: 30, noempty:true },*/

		]);
		
		
		// Create Dialog
		$('#dialog_container').dialog({
			closed:true,
			modal:true,
			title: '_',
			width:600,
			height: 500,
			buttons:[{
				text:'Save',
				iconCls:'icon-save',
				handler:function(){
					$('#form_master').submitForm('/'+controller+'/ajax_post', { onComplete : function(){ $('#dialog_container').dialog('close'); $('#datagrid').datagrid('reload'); }} );
				}
			},{
				text:'Close',
				iconCls:'icon-cancel',
				handler:function(){
					$('#dialog_container').dialog('close');
				}
			}]
		});
		
		
		// Set Action Button
		var buttons = [/* { label: 'Add', icon: 'add', onclick: function() {
							var oSel=$('#datagrid').datagrid('getSelected'); 
							if(oSel==null){ alert('Please select row!'); }
							else{ 
							resetError();
							$('#dialog_container').dialog('open');
							$('#form_master').data('mode', 'create');
							$('#form_master').parent().parent().parent().prev().find('.panel-title').html('New Customer Contract');
							//$('#kode_kontrak').readonly(false);
							//$('#kode_kontrak, #kode_produk, #kode_customer, #nama_customer, #tgl_awal_kontrak, #tgl_akhir_kontrak, #tgl_aktifasi #marketing_fee, #harga, #keterangan').val('');
							$('#kode_kontrak').focus();
							jsonToForm(oSel); $('#kode_produk').focus();
							}
						} },*/
						{ label: 'Edit', icon: 'edit', onclick: function() {
							var oSel=$('#datagrid').datagrid('getSelected'); 
							if(oSel==null){ alert('Please select row!'); }
							else{
								if(oSel['no_id']!=null){
									$('#no_id').setval(oSel['no_id']);
								} else {
									$.ajax({
									url:gs_path+'/'+controller+'/id_auto_null',
									type:'POST',
									dataType:'JSON',
									}).done(function(data){
										$('#no_id').setval(data['id_kontrak_auto']);
									});
								}
								
								resetError();
								$('#dialog_container').dialog('open'); 
								$('#form_master').data('mode', 'edit');
								$('#form_master').parent().parent().parent().prev().find('.panel-title').html('Edit Client Contract');
								$('#kode_kontrak').readonly(false);
								$('#nama_customer,#kode_customer').readonly();
								$('#id_customer').setval(oSel['id_customer']);
								jsonToForm(oSel); $('#kode_produk').focus();
							} 
						} },
						{label: 'Delete', icon: 'delete', onclick: function() { 
							var oSel = $('#datagrid').datagrid('getSelected'); 
							if (oSel == null) { 
								$.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); 
							} else { 
								deleteData(['no_id']);
							} 
						}}
						];
		setAction(buttons);
		
		
		$('#fm_search').focusFirst();
	});
//--><!]]></script>

<div id="tb" style="padding:5px;height:auto">
    <div>
        <form id="fm_search" onsubmit="grid_reload(); return false;">
        	<table class="tb_form" style="margin-bottom:0px">
            <tr>
            	<td width="100">Keyword</td>
                <td><input type="text" id="keyword" name="keyword" class="textfield medium" />
                	<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="grid_reload(); return false;">Search</a>
                </td>
            </tr>
            </table>
        </form>
    </div>
</div>
<div id="datagrid" toolbar="#tb"></div>



<div id="dialog_container" style="padding:5px;width:600px;height:380px;">
	<form id="form_master">
    <table class="tb_form" style="width:100%;" cellpadding="0" cellspacing="0">
    <tr>
        <td>
       		<input id="no_id" name="no_id" type="hidden"/>
       		<input id="id_customer" name="id_customer" type="hidden"/>
        </td>
    </tr>
    <tr>
        <td class="label" style="width:150px">Kode Kontrak</td>
        <td><input id="kode_kontrak" /></td>
    </tr>
    <tr>
        <td class="label">Kode Client</td>
        <td><input id="kode_customer" /></td>
    </tr>
    <tr>
        <td class="label">Nama Client</td>
        <td><input id="nama_customer" /></td>
    </tr>
    <tr>
        <td class="label">Kode Jenis</td>
        <td><input id="kode_jenis_barang" /></td>
    </tr>
    <tr>
        <td class="label">Nama Jenis</td>
        <td><input id="nama_jenis_barang" /></td>
    </tr>
    <tr>
        <td class="label">Bandwidth</td>
        <td><input id="bandwidth" /></td>
    </tr>
    <tr>
    	<td>Harga</td>
    	<td><input id="harga" /></td>
    </tr>
    <!-- <tr>
        <td class="label">Nama Produk</td>
        <td><input id="nama_inventori" /></td>
    </tr>
    <tr>
        <td class="label">Stock</td>
        <td><input id="stock" /></td>
    </tr>
    <tr>
        <td class="label">Jumlah</td>
        <td><input id="banyak" /></td>
    </tr>   
    <tr>
    	<td>Harga Satuan</td>
    	<td><input id="produk_harga" /></td>
    </tr> -->
    <!-- <tr>
        <td class="label">Biaya Pendaftaran</td>
        <td><input id="biaya_pendaftaran" /></td>
    </tr> -->
    <!-- <tr>
        <td class="label">Total Biaya</td>
        <td><input id="total_biaya" /></td>
    </tr> -->    
    <tr>
        <td class="label">Tgl Awal Kontrak</td>
        <td><input id="tgl_awal_kontrak" /></td>
    </tr> 
    <tr>
        <td class="label">Tgl Akhir Kontrak</td>
        <td><input id="tgl_akhir_kontrak" /></td>
    </tr>
    <tr>
        <td class="label">Tgl Aktifasi</td>
        <td><input id="tgl_aktifasi" /></td>
    </tr> 
    <tr>
        <td class="label">Marketing Fee</td>
        <td><input id="marketing_fee" /></td>
    </tr>
    <!-- <tr>
        <td class="label">Harga</td>
        <td><input id="harga" /></td>
    </tr>    -->                
    <tr>
        <td class="label">Keterangan</td>
        <td><input id="keterangan" /></td>
    </tr>       
    </table>
	</form>
</div>